package com.massa.caronafacil.activities;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import com.massa.caronafacil.R;
import com.massa.caronafacil.TabsViewPagerApplication;
import com.massa.caronafacil.db.SQLConnector;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TimePicker;

public class CriarCaronaActivity extends Activity implements OnClickListener {

	private int year;
	private int month;
	private int day;
	private int hour;
	private int minute;
	private Button btnChangeDate;
	private Button btnChangeTime;
	private Button btnOkCriarCarona;
	private Button btnMapa;

	static final int DATE_DIALOG_ID = 1;
	static final int TIME_DIALOG_ID = 2;

    @Override
    protected final void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Inflate the layout
        setContentView(R.layout.criar_carona_layout);

		btnChangeDate = (Button) findViewById(R.id.buttonDatePicker);
		btnChangeDate.setOnClickListener(this);
		btnChangeTime = (Button) findViewById(R.id.buttonTimePicker);
		btnChangeTime.setOnClickListener(this);
		btnOkCriarCarona = (Button) findViewById(R.id.buttonOkCriarCarona);
		btnOkCriarCarona.setOnClickListener(this);
		btnMapa = (Button) findViewById(R.id.buttonMapa);
		btnMapa.setOnClickListener(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected final void onSaveInstanceState(final Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

	@Override
	public void onClick(View v) {
        if (v != null) {
            switch (v.getId()) {
            case R.id.buttonOkCriarCarona:
            	criarCarona();
                break;
            case R.id.buttonDatePicker:
            	Dialog date = new Dialog(TabsViewPagerApplication.getAppContext(), DATE_DIALOG_ID);
            	date.show();
            	break;
            case R.id.buttonTimePicker:
            	Dialog time = new Dialog(TabsViewPagerApplication.getAppContext(), TIME_DIALOG_ID);
            	time.show();
            	break;
            case R.id.buttonMapa:
                final Intent intentMapa = new Intent(TabsViewPagerApplication.getAppContext(),
                		CriarCaronaGoogleMapsActivity.class);
                startActivity(intentMapa);
            	break;
            }
        }
	}

	public void criarCarona() {
		Connection conn = SQLConnector.getInstance().getConn();

		try {
			Statement stmt = conn.createStatement();
			ResultSet reset = stmt.executeQuery("select * from TableName");

			if(reset.next()){

			}
		} catch (Exception e) {
			Log.w("Error connection","" + e.getMessage());
		}
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DATE_DIALOG_ID:
		   // set date picker as current date
		   return new DatePickerDialog(this, datePickerListener, 
                         year, month, day);
		case TIME_DIALOG_ID:
			// set time picker as current time
			return new TimePickerDialog(this, 
                         timePickerListener, hour, minute, false);
 
		}
		return null;
	}
 
	private DatePickerDialog.OnDateSetListener datePickerListener 
                = new DatePickerDialog.OnDateSetListener() {
 
		// when dialog box is closed, below method will be called.
		public void onDateSet(DatePicker view, int selectedYear,
				int selectedMonth, int selectedDay) {
			year = selectedYear;
			month = selectedMonth;
			day = selectedDay;
		}
	};

	private TimePickerDialog.OnTimeSetListener timePickerListener = 
            new TimePickerDialog.OnTimeSetListener() {
		public void onTimeSet(TimePicker view, int selectedHour,
				int selectedMinute) {
			hour = selectedHour;
			minute = selectedMinute;
		}
	};
}
